package com.juyu.miliao.api.dao.redis;

import com.alibaba.fastjson.JSONObject;
import com.juyu.miliao.api.domain.vo.CoolSummerJackpotVO;
import com.juyu.miliao.api.domain.vo.CoolSummerRechargeAmountDetailVO;
import com.juyu.miliao.common.constant.CoolSummerActivitiesRedisKey;
import com.juyu.miliao.common.util.DoubleFormatUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

import javax.swing.text.DateFormatter;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @author xuanqicheng
 */
@Repository
public class CoolSummerActivitiesRedisDao {




    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 设置奖池
     * @param fee
     */
    public void updateJackpot(int fee) {
        stringRedisTemplate.opsForValue().increment(CoolSummerActivitiesRedisKey.getCoolSummerActivitiesJackpotKey(),
                fee*100);
    }

    /**
     * 获取奖池
     * @return
     */
    public CoolSummerJackpotVO getJackpot() {
        CoolSummerJackpotVO coolSummerJackpotVO = new CoolSummerJackpotVO();
        String target= stringRedisTemplate.opsForValue().get(CoolSummerActivitiesRedisKey.getCoolSummerActivitiesJackpotKey());
        if(StringUtils.isNotBlank(target)){
            coolSummerJackpotVO.setJackpot(Long.parseLong(target));
        }else {
            coolSummerJackpotVO.setJackpot(0L);
        }
        return coolSummerJackpotVO;
    }

    /**
     * 设置用户今日充值金额
     * @param userId
     * @param fee
     * @param localDateTime
     */
    public void updateUserRechargeAmount(long userId,double fee, LocalDateTime localDateTime){
        String key =CoolSummerActivitiesRedisKey.getCoolSummerActivitiesUserRechargeKey(userId,
                localDateTime.toLocalDate());
        stringRedisTemplate.opsForValue().increment(key,fee);
        stringRedisTemplate.expire(key,10, TimeUnit.DAYS);

    }

    /**
     * 设置用户活动期间充值金额
     * @param userId
     * @param fee
     * @param localDateTime
     */
    public void updateUserCountRechargeAmount(long userId,double fee, LocalDateTime localDateTime){
        String key =CoolSummerActivitiesRedisKey.getCoolSummerActivitiesUserCountRechargeKey(userId);
        stringRedisTemplate.opsForValue().increment(key,fee);
        stringRedisTemplate.expire(key,60, TimeUnit.DAYS);

    }

    /**
     * 获取用户活动期间充值金额
     * @param userId
     * @return
     */
    public double getUserCountRechargeAmount(long userId){
        String key =CoolSummerActivitiesRedisKey.getCoolSummerActivitiesUserCountRechargeKey(userId);
        String target = stringRedisTemplate.opsForValue().get(key);
        if(StringUtils.isNotBlank(target)){
            return Double.parseDouble(target);
        }else{
            return 0;
        }
    }

    /**
     * 设置用户活动期间充值明细
     * @param userId
     * @param fee
     * @param localDateTime
     */
    public void updateUserRechargeAmountDetail(long userId,double fee, LocalDateTime localDateTime){
        String detailKey = CoolSummerActivitiesRedisKey
                .getCoolSummerActivitiesUserRechargeDetailKey(userId);
        CoolSummerRechargeAmountDetailVO detailVO = new CoolSummerRechargeAmountDetailVO();
        detailVO.setRechargeAmount("充值"+DoubleFormatUtil.doubleFormate(fee)+"元");
        DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        detailVO.setTime(timeFormatter.format(localDateTime));

        stringRedisTemplate.opsForZSet().add(detailKey, JSONObject.toJSONString(detailVO),
                localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli());

        stringRedisTemplate.expire(detailKey,60,TimeUnit.DAYS);
    }


    /**
     * 获取用户活动期间充值记录
     * @param userId
     * @param startNum
     * @param endNumber
     * @return
     */
    public List<CoolSummerRechargeAmountDetailVO> getCoolSummerRechargeAmountDetailVOList(long userId,
                                                                                          int startNum, int endNumber){
        String key = CoolSummerActivitiesRedisKey
                .getCoolSummerActivitiesUserRechargeDetailKey(userId);

        Set<String> set = stringRedisTemplate.opsForZSet().reverseRange(key, startNum, endNumber);

        List<CoolSummerRechargeAmountDetailVO> resultList = new ArrayList<>();
        if(set!=null&&set.size()>0){
            List<String> list = new ArrayList<>(set);
            for(String str : list){
                CoolSummerRechargeAmountDetailVO vo = JSONObject.parseObject(str,
                        CoolSummerRechargeAmountDetailVO.class);
                resultList.add(vo);
            }
        }

        return resultList;
    }

    /**
     * 获取用户今日充值金额
     * @param userId
     * @param day
     * @return
     */
    public double getUserRechargeAmount(long userId,LocalDate day){
        String key =CoolSummerActivitiesRedisKey.getCoolSummerActivitiesUserRechargeKey(userId,day);
        String target=stringRedisTemplate.opsForValue().get(key);
        if(StringUtils.isNotBlank(target)){
            return Double.parseDouble(target);
        }else{
            return 0;
        }
    }



    /**
     * 检查用户是否领取奖励
     * true 表示已经领取过，false 表示没有领取过
     */
    public boolean checkUserGetRewards(long userId,LocalDate day,int activitiesId){
        String key = CoolSummerActivitiesRedisKey.getCoolSummerAcitvitiesGetRewardsKey(day,activitiesId);
        boolean bl = stringRedisTemplate.opsForSet().isMember(key,userId+"");
        return bl;
    }

    /**
     * 设置用户领取奖励记录
     */
    public long addUserGetRewards(long userId,LocalDate day,int activitiesId){
        String key = CoolSummerActivitiesRedisKey.getCoolSummerAcitvitiesGetRewardsKey(day,activitiesId);
        long num=stringRedisTemplate.opsForSet().add(key,userId+"");
        stringRedisTemplate.expire(key,10,TimeUnit.DAYS);
        return num;
    }


    public void removeUserGetRewards(long userId, LocalDate day, int activitiesId) {
        String key = CoolSummerActivitiesRedisKey.getCoolSummerAcitvitiesGetRewardsKey(day,activitiesId);
        stringRedisTemplate.opsForSet().remove(key,userId+"");
    }

    /**
     * 统计用户领取奖励
     * @param userId
     * @param day
     * @param activitiesId
     */
    public void statisticsUserGetRewards(long userId,LocalDate day,int activitiesId){
        String  key = CoolSummerActivitiesRedisKey.getCoolSummerActivitiesStatisticsGetRewardsKey(day,activitiesId);
        stringRedisTemplate.opsForHyperLogLog().add(key,userId+"");
        stringRedisTemplate.expire(key,10,TimeUnit.DAYS);
    }

}
